﻿package yii.db.schema{
	import phpas3.*;

	public class CDbColumnSchema{



	/**
	 * @var string name of this column (without quotes)+
	 */
	public var name:String;
	/**
	 * @var string raw name of this column+ This is the quoted name that can be used in SQL queries+
	 */
	public var rawName:String;
	/**
	 * @var boolean whether this column can be null+
	 */
	public var allowNull:Boolean;
	/**
	 * @var string the DB type of this column+
	 */
	public var dbType:String;
	/**
	 * @var string the PHP type of this column+
	 */
	public var type:String;
	/**
	 * @var mixed default value of this column
	 */
	public var defaultValue:*;
	/**
	 * @var integer size of the column+
	 */
	public var size:int;
	/**
	 * @var integer precision of the column data, if it is numeric+
	 */
	public var precision:int;
	/**
	 * @var integer scale of the column data, if it is numeric+
	 */
	public var scale:int;
	/**
	 * @var boolean whether this column is a primary key
	 */
	public var isPrimaryKey:Boolean;
	/**
	 * @var boolean whether this column is a foreign key
	 */
	public var isForeignKey:Boolean;
	/**
	 * @var boolean whether this column is auto-incremental
	 * @since 1+1+7
	 */
	public var autoIncrement:Boolean=false;




	/**
	 * Extracts size, precision and scale information from column's DB type+
	 * @param string $dbType the column's DB type
	 */
	protected function extractLimit( $dbType:String ):void{
		var $matches:Array = new Array();
		if(strpos($dbType,'(') && preg_match(/\((+*)\)/,$dbType,$matches)){
			var $values = explode( ',', $matches[1] );
			this.size = this.precision = int( $values[0] );
			if( isset($values[1]) )
				this.scale = int( $values[1] );
		}
	}




	/**
	 * Extracts the PHP type from DB type+
	 * @param string $dbType DB type
	 */
	protected function extractType( $dbType:String ){
		if(stripos($dbType,'int')!==false && stripos($dbType,'unsigned int')===false)
			this.type='integer';
		else if(stripos($dbType,'bool')!==false)
			this.type='boolean';
		else if(preg_match(/(real|floa|doub)/i,$dbType))
			this.type='double';
		else
			this.type='string';
	}



	/**
	 * Converts the input value to the type that this column is of+
	 * @param mixed $value input value
	 * @return mixed converted value
	 */
	public function typecast( $value:* ):*{
		if( gettype($value)===this.type || $value===null || $value is CDbExpression ){
			return $value;
		}
		if($value===''){
			return this.type==='string' ? '' : null;
		}
		switch(this.type){
			case 'string': return String( $value );
			case 'integer': return int( $value );
			case 'boolean': return Boolean( $value );
			case 'double':
			default: return $value;
		}
	}




	/**
	 * Extracts the default value for the column+
	 * The value is typecasted to correct PHP type+
	 * @param mixed $defaultValue the default value obtained from metadata
	 */
	protected function extractDefault( $defaultValue:* ):void{
		this.defaultValue = this.typecast($defaultValue);
	}



	/**
	 * Initializes the column with its DB type and default value+
	 * This sets up the column's PHP type, size, precision, scale as well as default value+
	 * @param string $dbType the column's DB type
	 * @param mixed $defaultValue the default value
	 */
	public function init( $dbType:String, $defaultValue:*=null ):void{
		this.dbType = $dbType;
		this.extractType( $dbType );
		this.extractLimit($dbType);
		if( $defaultValue )
			this.extractDefault($defaultValue);
	}
















	}
}